package com.stgl.action;

import com.alibaba.fastjson.JSONArray;
import com.stgl.dao.BmqkDao;
import com.stgl.service.WhkfdbService;
import com.stgl.util.ExcelUtils;
import com.stgl.util.IDCardUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;

@Controller
@EnableAutoConfiguration
@RequestMapping("/whk")
public class whkfdb {
    @Resource
    private WhkfdbService whkfdbService;


    @RequestMapping("Search_whkfdb.html")
    @ResponseBody
    public Map<String,Object>Search_whkfdb(HttpServletRequest request, HttpServletResponse response){
        String nj=request.getParameter("nj").trim();
        String bj=request.getParameter("bj").trim();
        String km="";
        if(request.getParameter("km")!=null){
            km=request.getParameter("km").trim();
        }
        String mc="";
        if(request.getParameter("mc")!=null){
            mc=request.getParameter("mc").trim();
        }

        String wstate=request.getParameter("wstate").trim();
        String bmlb=request.getParameter("bmlb").trim();

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("nj",nj);
        gmap.put("bj",bj);
        gmap.put("km",km);
        gmap.put("mc",mc);
        gmap.put("wstate",wstate);
        gmap.put("bmlb",bmlb);
        List<Map<String,Object>>list1=whkfdbService.Search_whkfdb(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
        return rmap;
    }


    @RequestMapping("del_whkfdb.html")
    @ResponseBody
    public Map<String,Object> del_whkfdb(HttpServletRequest request, HttpServletResponse response){
        String wid=request.getParameter("wid").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("wid",wid);
        gmap.put("wsid",wid);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap=whkfdbService.del_whkfdb(gmap);
        return rmap;
    }


    @RequestMapping("InsertWhkfdb.html")
    @ResponseBody
    public Boolean InsertWhkfdb(HttpServletRequest request, HttpServletResponse response){
        String nj="";
        if(request.getParameter("nj")!=null){
            nj=request.getParameter("nj").trim();
        }

        String bj="";
        if(request.getParameter("bj")!=null){
            bj=request.getParameter("bj").trim();
        }

        String km="";
        if(request.getParameter("km")!=null){
            km=request.getParameter("km").trim();
        }

        String mc="";
        if(request.getParameter("mc")!=null){
            mc=request.getParameter("mc").trim();
        }

        String jj="";
        if(request.getParameter("jj")!=null){
            jj=request.getParameter("jj").trim();
        }

        String fy="";
        if(request.getParameter("fy")!=null){
            fy=request.getParameter("fy").trim();
        }

        String xe="";
        if(request.getParameter("xe")!=null){
            xe=request.getParameter("xe").trim();
        }

        String sksj="";
        if(request.getParameter("sksj")!=null){
            sksj=request.getParameter("sksj");
        }

        String skdd="";
        if(request.getParameter("skdd")!=null){
            skdd=request.getParameter("skdd").trim();
        }

        String bmzlsm="";
        if(request.getParameter("bmzlsm")!=null){
            bmzlsm=request.getParameter("bmzlsm").trim();
        }

        String sfjzsj="";
        if(request.getParameter("sfjzsj")!=null){
            sfjzsj=request.getParameter("sfjzsj").trim();
        }

        String bmlb="";
        if(request.getParameter("bmlb")!=null){
            bmlb=request.getParameter("bmlb").trim();
        }

        String whksf="";
        if(request.getParameter("whksf")!=null){
            whksf=request.getParameter("whksf");
        }

        String whk_teacher="";
        if(request.getParameter("whk_teacher")!=null){
            whk_teacher=request.getParameter("whk_teacher");
        }

        List<Map<String,String>> whksflist = (List<Map<String,String>>) JSONArray.parse(whksf);
        List<Map<String,String>> wtlsit = (List<Map<String,String>>) JSONArray.parse(whk_teacher);

        Map<String,Object>gmap=new HashMap<String,Object>();
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("csid")!=null){
            gmap.put("csid",session.getAttribute("csid").toString());
        }else{
            return false;
        }

        String wid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("wid",wid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("jlxzsj",df.format(new Date()));
        gmap.put("nj",nj);
        gmap.put("bj",bj);
        gmap.put("km",km);
        gmap.put("mc",mc);
        gmap.put("jj",jj);
        gmap.put("fy",fy);
        gmap.put("xe",xe);
        gmap.put("sksj",sksj);
        gmap.put("skdd",skdd);
        gmap.put("bmzlsm",bmzlsm);
        gmap.put("xtstate","0");
        gmap.put("sfjzsj",sfjzsj);
        gmap.put("bmlb",bmlb);

        int i=whkfdbService.InsertWhkfdb(gmap);
        if(i>=0){
            for(int m=0;m<whksflist.size();m++){
                String fid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("fid",fid);
                whkmap.put("wid",wid);
                whkmap.put("xmmc",whksflist.get(m).get("xmmc"));
                whkmap.put("fee",whksflist.get(m).get("fee"));
                whkfdbService.InsertWhksf(whkmap);
            }
            for(int m=0;m<wtlsit.size();m++){
                String wtid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("wtid",wtid);
                whkmap.put("wid",wid);
                whkmap.put("tid",wtlsit.get(m).get("tid"));
                whkfdbService.InsertWhk_teacher(whkmap);
            }
            return true;
        }else{
            return false;
        }
    }




    @RequestMapping("EditWhkfdb.html")
    @ResponseBody
    public Boolean EditWhkfdb(HttpServletRequest request, HttpServletResponse response){
        String wid=request.getParameter("wid").trim();

        String nj="";
        if(request.getParameter("nj")!=null){
            nj=request.getParameter("nj").trim();
        }

        String bj="";
        if(request.getParameter("bj")!=null){
            bj=request.getParameter("bj").trim();
        }

        String km="";
        if(request.getParameter("km")!=null){
            km=request.getParameter("km").trim();
        }

        String mc="";
        if(request.getParameter("mc")!=null){
            mc=request.getParameter("mc").trim();
        }

        String jj="";
        if(request.getParameter("jj")!=null){
            jj=request.getParameter("jj").trim();
        }

        String fy="";
        if(request.getParameter("fy")!=null){
            fy=request.getParameter("fy").trim();
        }

        String xe="";
        if(request.getParameter("xe")!=null){
            xe=request.getParameter("xe").trim();
        }

        String sksj="";
        if(request.getParameter("sksj")!=null){
            sksj=request.getParameter("sksj");
        }

        String skdd="";
        if(request.getParameter("skdd")!=null){
            skdd=request.getParameter("skdd").trim();
        }

        String bmzlsm="";
        if(request.getParameter("bmzlsm")!=null){
            bmzlsm=request.getParameter("bmzlsm").trim();
        }

        String sfjzsj="";
        if(request.getParameter("sfjzsj")!=null){
            sfjzsj=request.getParameter("sfjzsj").trim();
        }

        String whksf="";
        if(request.getParameter("whksf")!=null){
            whksf=request.getParameter("whksf");
        }

        String whk_teacher="";
        if(request.getParameter("whk_teacher")!=null){
            whk_teacher=request.getParameter("whk_teacher");
        }

        List<Map<String,String>> whksflist = (List<Map<String,String>>) JSONArray.parse(whksf);
        List<Map<String,String>> wtlsit = (List<Map<String,String>>) JSONArray.parse(whk_teacher);

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("wid",wid);
        gmap.put("nj",nj);
        gmap.put("bj",bj);
        gmap.put("km",km);
        gmap.put("mc",mc);
        gmap.put("jj",jj);
        gmap.put("fy",fy);
        gmap.put("xe",xe);
        gmap.put("sksj",sksj);
        gmap.put("skdd",skdd);
        gmap.put("bmzlsm",bmzlsm);
        gmap.put("sfjzsj",sfjzsj);
        int i=whkfdbService.EditWhkfdb(gmap);
        if(i>=0){
            for(int m=0;m<whksflist.size();m++){
                String fid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("fid",fid);
                whkmap.put("wid",wid);
                whkmap.put("xmmc",whksflist.get(m).get("xmmc"));
                whkmap.put("fee",whksflist.get(m).get("fee"));
                whkfdbService.InsertWhksf(whkmap);
            }
            for(int m=0;m<wtlsit.size();m++){
                String wtid = UUID.randomUUID().toString().replaceAll("-", "");
                Map<String,Object>whkmap=new HashMap<String,Object>();
                whkmap.put("wtid",wtid);
                whkmap.put("wid",wid);
                whkmap.put("tid",wtlsit.get(m).get("tid"));
                whkfdbService.InsertWhk_teacher(whkmap);
            }
            return true;
        }else{
            return false;
        }
    }



    @RequestMapping("delHistoryWhkfdb.html")
    @ResponseBody
    public boolean delHistoryWhkfdb(HttpServletRequest request, HttpServletResponse response){
        String wids=request.getParameter("wids").trim();
        List<Map<String,String>> listw = (List<Map<String,String>>) JSONArray.parse(wids);
        Map<String,Object>gmap=new HashMap<String,Object>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String jlscsj=df.format(new Date());
        for(int i=0;i<listw.size();i++){
            gmap.put("jlscsj",jlscsj);
            String wid=listw.get(i).get("wid").toString();
            gmap.put("wid",wid);
            whkfdbService.delHistoryWhkfdb(gmap);
        }
        return true;
    }



    //恢复历史报名
    @RequestMapping("resHistoryWhkfdb.html")
    @ResponseBody
    public boolean resHistoryWhkfdb(HttpServletRequest request, HttpServletResponse response){
        String wids=request.getParameter("wids").trim();
        List<Map<String,String>> listw = (List<Map<String,String>>) JSONArray.parse(wids);
        Map<String,Object>gmap=new HashMap<String,Object>();
        for(int i=0;i<listw.size();i++){
            String wid=listw.get(i).get("wid").toString();
            gmap.put("wid",wid);
            whkfdbService.resHistoryWhkfdb(gmap);
        }
        return true;
    }


}
